<template>
	<view class="wrap">
		<u-gap height="20" bg-color="#f5f5f5"></u-gap>
		<div class="version-logo"><image src="/static/assets/images/logo.png" class="logo-image" /></div>
    <div class="version-text" @click="handleClick">当前版本 {{ version }}</div>
    <div class="version-update">
      <u-cell-group class="cell-group">
        <u-cell-item title="检查更新" @click="checkUpdate" :arrow="true"></u-cell-item>
      </u-cell-group>
    </div>
		<uni-popup ref="process" type="center">
			<view class="popup-content">
				<progress :percent="progress" stroke-width="8" active-color="#5b95ff" style="overflow: hidden;border-radius: 6px"></progress>
			</view>
			<view style="text-align: center;color: #fff;padding: 10px;">
				{{ '下载中: ' +  progress + '%'}}
			</view>
		</uni-popup>
	</view>
</template>

<script>
import { TimesTrigger, loger } from '@daelui/dogjs/dist/components.js'
import { queryAppVersion } from '@/api/system/app.js'

export default {
	data() {
		return {
			version: '1.0.0',
			versionCode: 1,
			progress: 0
		}
	},

  onShow () {
		const systemInfo = uni.getSystemInfoSync()
		const currentVersion = systemInfo.appVersion
    this.version = currentVersion
		const currentVersionCode = parseFloat(systemInfo.appVersionCode || 0)
    this.versionCode = currentVersionCode
  },

	methods: {
		// 检查更新
		checkUpdate() {
			// #ifdef APP-PLUS
			queryAppVersion().then(res => {
				res = res || {}
				// 本地版本与查询的版本不一致则提示更新
				if (String(res.code) === '200') {
					if (this.versionCode >= String(res.data?.version)) {
						this.$u.toast('已是最新版本，无需更新')
						return true
					}
					uni.showModal({
						title: '提示',
						content: '是否下载更新？',
						showCancel: true,
						success: (res2) => {
							if (res2.confirm) {
								this.download(res.data)
							}
						}
					})
				} else {
					this.$u.toast(res.message || '检查更新失败')
				}
			}).catch(() => {
				this.$u.toast('检查更新失败')
			})
			// #endif
			// #ifndef APP-PLUS
			this.$u.toast('小程序端或H5端无需检查更新')
			// #endif
		},
		// 下载资源
    download (data) {
			let url = this.vuex_config.baseUrl + '/common/files/download?resource='
				+ encodeURIComponent(data.downloadUrl)
				+ '&downloadFilename=yhxfz-1.apk'
			// 下载并安装
			let downloadTask = uni.downloadFile({
				url: url,
				success: (downloadResult) => {
					if (downloadResult.statusCode === 200) {
						plus.runtime.install(downloadResult.tempFilePath, {
							force: false
						}, function() {
							plus.runtime.restart()
						}, function(e) {
							loger.error('Install fail...')
						})
					}
				},
				complete: () => {
					this.$refs.process.close()
					this.progress = 0
				}
			})
			downloadTask.onProgressUpdate(res => {
				this.$refs.process.open()
				this.progress = res.progress
			})
			// 仅下载
			// plus.runtime.openURL(url)
		},
		// Debug模式触发
		handleClick () {
      let tt = this.tt
      if (!tt) {
        tt = new TimesTrigger({
          times: 5
        })
        this.tt = tt
      }
      tt.trigger(function() {
        uni.navigateTo({
          url: '/pages/sys/workbench/debug'
        })
      })
    }
	}
};
</script>
<style lang="scss">
@import '../home/index.scss';
page {
	background-color: #fff;
}
.version-logo {
  padding: 16px 0;
  text-align: center;
  .logo-image {
    max-width: 80%;
    // width: 128px;
    // height: 128px;
    width: 86px;
    height: 86px;
  }
}
.version-text {
  padding: 8px 0;
  text-align: center;
}
.version-update {
  margin-top: 24px;
  padding: 16px 0;
}
</style>
